package com.annotation;

import java.lang.reflect.Field;

/**
 * 使用反射读取注解信息，模拟处理注解信息流程
 *
 * @author jiangbang
 * @date 2016/8/30
 */
public class SxtDemo {


    /**
     * Description: 生成建表语句
     *
     * @return
     * @author jiangbang
     * @date 2018/9/8
     */
    public static void main(String[] args) throws ClassNotFoundException {
        StringBuffer createSql = new StringBuffer();
        Class clazz = Class.forName("com.annotation.SxtStudent");

        // 判断类上是否存在注解，并获取类上面注解的实例
        if (clazz.isAnnotationPresent(SxtTable.class)) {
            SxtTable sxtTable = (SxtTable) clazz.getAnnotation(SxtTable.class);
            createSql.append("CREATE TABLE ").append(sxtTable.value()).append(" { \n");
        }

        // 判断Field上是否存在注解，并获取类上面注解的实例
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            SxtField sxtField = field.getAnnotation(SxtField.class);
            createSql.append(sxtField.name()).append(" ")
                    .append(sxtField.type()).append("(").append(sxtField.length()).append("), \n");
        }

        createSql.append("PRIMARY KEY (`id`) \n };");

        System.err.println(createSql.toString());
    }
}
